package com.neutech.util;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.neutech.core.BaseEntity;
import com.zaxxer.hikari.HikariDataSource;

public class AutoCoding {
    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://192.168.80.191:3306/teach?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }
    public static GlobalConfig createGlobalConfigUseStyle() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        String table = "home_work_commit";
        // 配置路径
        String parentPath = System.getProperty("user.dir");
        String sourceDir = parentPath + "/src/main/java";
        //设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.neutech")
                .setSourceDir(sourceDir)
                .setMapperXmlPath(parentPath + "/src/main/resources/com/neutech/mapper");
        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setGenerateTable(table);
        //设置生成 entity 并启用 Lombok
        globalConfig
                .enableEntity()
                .setWithLombok(true)
                .setWithSwagger(true)
                .setJdkVersion(17)
                .setSwaggerVersion(EntityConfig.SwaggerVersion.DOC)
                .setSuperClass(BaseEntity.class);
        //设置生成 mapper
        globalConfig.enableMapper();
        // 设置生成xml
        globalConfig.enableMapperXml();
        // 设置生成service
        globalConfig.enableService();
        // 设置生成service实现
        globalConfig.enableServiceImpl();
        // 设置生成controller
        globalConfig.enableController()
                .setRestStyle(true);
        return globalConfig;
    }
}
